package com.jiesen.base.utils;

import com.jiesen.base.modules.certificate.textbook.model.TextBook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将一个列表根据父类id转换为tree字符串
 */
public class ListByParentToTree {


    //正在完成
    public static HashMap<String,Object> ListTreeToHasMap(List<Object> list,String id,String parentName){
        if (list==null||list.size()==0){
            return null;
        }
        HashMap<Object,Object> map = new HashMap<>();
        for (Object o : list) {

            boolean father = true;

            HashMap<String, Object> to = ObjectToMap.to(o);

            //获取当前节点的父类id
            Object parent = to.get(parentName);

            for (Object o1 : list) {
                //获取当前节点的id
                Object nid = to.get(id);

                if(nid.equals(parent)){
                    father=false;
                }

            }

            //获取当前节点的id
            Object nid = to.get(id);


            map.put(nid,to);
        }
        for (Map.Entry<Object, Object> objectObjectEntry : map.entrySet()) {
            System.out.println(objectObjectEntry.getKey()+" = "+objectObjectEntry.getValue());
        }
        return null;
    }

    public static void main(String[] args) {
        List textBooks = new ArrayList<>();

        textBooks.add(new TextBook(1, 0, "证书中心", 1));
        textBooks.add(new TextBook(3, 1, "教材管理", 1));
        textBooks.add(new TextBook(4, 1, "教材管理", 2));
        textBooks.add(new TextBook(2, 0, "用户中心", 1));
        textBooks.add(new TextBook(5, 2, "用户管理", 1));

        ListTreeToHasMap(textBooks,"id","parentId");

    }
}
